% Compute figures for Ritschl, Sarferaz, and Uebele (2014)
clear all
clc

addpath('funcs');
addpath('scrpts');

%% Load and save data for plots
nipa_level      = load('data/us_gdp_1929_2008.txt');
nipa_loglevel   = log(nipa_level(2:end-2,2));
data_pre1930    = load('data/us_balke_gordon_romer_nominal_real.txt');
romer_loglevel  = log(data_pre1930(:,4));
bg_loglevel     = log(data_pre1930(:,2));
data_kuznets    = load('data/us_gnp_1925_1955_Kuznets.txt');
kuznets_level   = data_kuznets(:,3);
kuznets_loglevel= log(kuznets_level(11:end));

gdp_loglevel    = [romer_loglevel; nipa_loglevel];

cpi_level       = load('data/us_cpi_1875_2007.txt');
cpi_loglevel    = log(cpi_level(1:132));

[nipa_trend, nipa]      =   hpfilter(nipa_loglevel,6.25);
[cpi_trend, cpi]        =   hpfilter(cpi_loglevel,6.25);
[romer_trend, romer]    =   hpfilter(romer_loglevel,6.25);
[bg_trend, bg]          =   hpfilter(bg_loglevel,6.25);
%[gdp_trend, gdp]        =   hpfilter(gdp_loglevel,6.25);
gdp                     =  [romer; nipa];
[kuz_trend, kuz_55]     =   hpfilter(kuznets_loglevel,6.25);
kuz_ww                  = kuz_55(1:21);

nipa_ll_ww              =  log(nipa_level(1:end,2));
[nipa_trendww, nipa_55] =   hpfilter(nipa_ll_ww,6.25);

nipa_ww                 = nipa_55(1:21);

std_nipa                = std(gdp(78:end));
std_cpi                 = std(cpi(72:end));
std_nipaww              = std(nipa_ww);
std_kuzww              = std(kuz_ww);

save data/data_gdp gdp cpi romer_loglevel bg_loglevel std_nipa std_cpi...
    std_nipaww std_kuzww romer bg nipa nipa_ww kuz_ww

%% Plot factor, factor loadings and stochastic volatilities for 52 series 
filename='matFiles/intermediate/results_52_1867_2006_all_intermediate';
load(filename);

load('data/data_gdp');
name53_1867_1995_new
bands = 1;
figall = 1;
large=0;
tt  =   1867:2006;
outputfilename = 'figures/factor_bands_all.pdf';
std_data = std_nipa;
plotit

%% Plot factor against GDP (Romer + NIPA)
clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_all_intermediate';
load(filename);

bands = 0;
figall = 0;
large=0;

load('data/data_gdp');

tt_gdp = 1869:2006;
tt  =   1867:2006;
data_s = gdp;
legend_name = 'GNP';
legend_factor = 'Factor';
outputfilename = 'figures/factor_nipa_romer_1867_2006.pdf';
std_data = std_nipa;
plotit

%% Plot factor for 1867-1929 (98 series) against GDP (Romer)/ GDP (Balke-Gordon)

% Romer vs. Factor
clear all; clc;
filename='matFiles/intermediate/results_US_98_1867_1929_intermediate';
load(filename);

bands = 0;
figall = 0;
large=1;
war=0;

load('data/data_gdp');
first = 1;
last = 63;
tt_gdp = 1869:1929;
tt  =   1867:1929;
data_s = romer;
legend_name = 'Romer GNP';
legend_factor = ['Factor (' num2str(size(yt,1)) ' Series)'];

outputfilename = 'figures/factor_romer_1867_1929_98.pdf';
std_data = std(romer);
plotit

% Balke-Gordon vs. Factor
clear all; clc;
filename='matFiles/intermediate/results_US_98_1867_1929_intermediate';
load(filename);

bands = 0;
figall = 0;
large=1;
war=0;


load('data/data_gdp');

first = 1;
last = 63;
tt_gdp = 1869:1929;
tt  =   1867:1929;
data_s = bg;
legend_name = 'Balke-Gordon GNP';
legend_factor = ['Factor (' num2str(size(yt,1)) ' Series)'];

outputfilename = 'figures/factor_bg_1867_1929_98.pdf';
std_data = std(bg);
plotit

%% Plot factor for 1867-1929 (52 series) against GDP (Romer)/ GDP (Balke-Gordon)

% Romer vs. Factor
clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_all_intermediate';
load(filename);

bands = 0;
figall = 0;
large=1;
war=0;


load('data/data_gdp');

first = 1;
last = 63;
tt_gdp = 1869:1929;
tt  =   1867:1929;
data_s = romer;
legend_name = 'Romer GNP';
legend_factor = 'Factor (52 Series)';

outputfilename = 'figures/factor_romer_1867_1929_52.pdf';
std_data = std(romer);
plotit

% Balke-Gordon vs. Factor
clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_all_intermediate';
load(filename);

bands = 0;
figall = 0;
large=1;
war=0;


load('data/data_gdp');

first = 1;
last = 63;
tt_gdp = 1869:1929;
tt  =   1867:1929;
data_s = bg;
legend_name = 'Balke-Gordon GNP';
legend_factor = 'Factor (52 Series)';

outputfilename = 'figures/factor_bg_1867_1929_52.pdf';
std_data = std(bg);
plotit

%% Plot nominal factor (17 series) against CPI

clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_nom_intermediate';
load(filename);

%results.ft=-results.ft;
bands = 0;
figall = 0;
large=0;

load('data/data_gdp');

tt_gdp = 1874:2005;
tt  =   1867:2006;
data_s = cpi;
legend_name = 'CPI';
legend_factor = 'Factor';
outputfilename = 'figures/factor_nom_cpi_1867_2006.pdf';
std_data = std_cpi;
plotit

%% Plot real factor against NIPA/Kuznets

% Real factor vs. NIPA 
clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_real_intermediate';
load(filename);
results.ft=-results.ft;

bands = 0;
figall = 0;
large=1;
war=1;

load('data/data_gdp');
first = 63;
last  = 83;

tt_gdp = 1929:1949;
tt  =   1929:1949;
data_s = nipa_ww;
legend_name = 'NIPA';
legend_factor = 'Factor';
outputfilename = 'figures/wargnp_nipa.pdf';
std_data = std_nipaww;
plotit

% % Real factor vs. Kuznets
clear all; clc;
filename='matFiles/intermediate/results_52_1867_2006_real_intermediate';
load(filename);
results.ft=-results.ft;

bands = 0;
figall = 0;
large=1;
war=1;

load('data/data_gdp');
first = 63;
last  = 83;

tt_gdp = 1929:1949;
tt  =   1929:1949;
data_s = kuz_ww;
legend_name = 'Kuznets';
legend_factor = 'Factor';
outputfilename = 'figures/wargnp_kuznets.pdf';
std_data = std_kuzww;
plotit